package com.easysale.controllers;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easysale.domain.Product;
import com.easysale.services.ProductService;

@Controller
public class HomeController extends EasySaleController {

    protected static Logger logger = Logger.getLogger("HomeController");

    @Autowired
    ProductService productService;
    
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String printHome(ModelMap model) {
    	model.put("product", new Product());
    	model.put("products", productService.products());
        model.put("homeActive", true);
        return "home-tiles";
    }

    @RequestMapping(value = "/addProduct", method = RequestMethod.POST)
    public String addProduct(@ModelAttribute("product")
    Product product, BindingResult result) {
 
        productService.addProduct(product);
 
        return "redirect:/";
    }
    
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(ModelMap model, HttpSession session,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        return "redirect:/";
    }

}
